﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using WmsApp.WmsService;

namespace WmsApp.Security
{
    public class UserSession
    {
        public const string AdminUser = "admin";
        public const string PrixToken = "token:";
        public static SystemUser CurrentUser { get; private set; }
        public static string Token { get; private set; }
        public static bool IsLogOn { get; private set; }
        public static string Login(string userID, string password)
        {
            string result = Factory.WmsServiceFactory.GetServiceClient().Login(userID, password);

            if (result.StartsWith(PrixToken))
            {
                IsLogOn = true;
                Token = result.Substring(6);
                CurrentUser =  Factory.WmsServiceFactory.GetServiceClient().GetSystemUser(userID);
            }
            return result;
        }
        public static bool CheckVersion()
        {
            string lowestVersion = Factory.WmsServiceFactory.GetServiceClient().GetLowestVersion();
            string localVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            if (String.Compare(localVersion.Substring(0, 1), lowestVersion) < 0)
            {
                Exceptions.ExceptionHandler.ShowException("本地程序版本太老，请更新程序后再试试！");
                return false;
            }
            return true;
        }
    }
}
